package com.magic.ch07_贪心.股票买卖II;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

/**
 * created by mzk on 2020/07/17
 */
public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter pw = new PrintWriter(System.out);

        int n = Integer.parseInt(br.readLine());
        String[] s = br.readLine().split(" ");
        br.close();
        int ans = 0;
        int[] price = new int[n + 1];
        for (int i = 0; i < n; i++) {
            price[i] = Integer.parseInt(s[i]);
        }
        for (int i = 0; i + 1 < n; i++) {
            int dt = price[i + 1] - price[i];
            if (dt > 0) {
                ans += dt;
            }
        }
        pw.print(ans);
        pw.flush();
        pw.close();
    }
}
